package com.biz.primus.service.push.util;

import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import com.biz.primus.base.enums.push.NotifyPlatform;
import com.biz.primus.base.enums.push.NotifyType;
import com.biz.primus.base.vo.push.Notification;
import com.biz.primus.base.vo.push.PushMessage;
import com.google.common.collect.Lists;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.TargetedMessage;
import java.util.List;
import org.springframework.util.StringUtils;


public class PushUtil {

    public static PushPayload buildJPushPayload(Notification notification, Boolean isProduction) {
        List<String> targets = Lists.newArrayList();
        if (notification.getNotifyType() == NotifyType.P2P) {
            targets.add(notification.getTarget());
        }

        PushMessage pushMessage = notification.getPushMessage();
        if (notification.getPlatform().equals(NotifyPlatform.IOS)) {
            return buildJpushIosPayLoad(pushMessage, targets, isProduction);
        } else if (notification.getPlatform().equals(NotifyPlatform.ANDROID_IOS)) {
            return buildJPushAndroidAndIosPayload(pushMessage, targets);
        }
        return buildJpushAndroidPayLoad(pushMessage, targets);
    }

    public static PushPayload buildJPushAndroidAndIosPayload(PushMessage pushMessage, List<String> targets) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(targets.size() > 0 ? Audience.alias(targets) : Audience.all())
                .setNotification(cn.jpush.api.push.model.notification.Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(pushMessage.getAlertMsg())
                                .setBadge(pushMessage.getBadge())
                                .setSound(pushMessage.getSound())
                                .addExtra("url", pushMessage.getUrl())
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(pushMessage.getAlertMsg())
                                .setTitle(Notification.NOTIFICATION_TITLE)
                                .addExtra("url", pushMessage.getUrl())
                                .build())
                        .build())
                .build();
    }

    public static PushPayload buildJpushIosPayLoad(PushMessage pushMessage, List<String> targets,
                                                   Boolean isProduction) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(targets.size() > 0 ? Audience.alias(targets) : Audience.all())
                .setNotification(cn.jpush.api.push.model.notification.Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(pushMessage.getAlertMsg())
                                .setBadge(pushMessage.getBadge())
                                .setSound(pushMessage.getSound())
                                .addExtra("url", pushMessage.getUrl())
                                .build())
                        .build()).setOptions(Options.newBuilder().setApnsProduction(isProduction).build())
                .build();
    }

    public static PushPayload buildJpushAndroidPayLoad(PushMessage pushMessage, List<String> targets) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(targets.size() > 0 ? Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(targets))
                        .build() : Audience.all())
                .setNotification(cn.jpush.api.push.model.notification.Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(pushMessage.getAlertMsg())
                                .setTitle(Notification.NOTIFICATION_TITLE)
                                .addExtra("url", pushMessage.getUrl())
                                .build())
                        .build())
                .build();
    }

    public static List<TargetedMessage> buildMiTargetPushMessages(Notification notification) {
        List<String> targets = Lists.newArrayList();

        targets.add(notification.getTarget());

        PushMessage pushMessage = notification.getPushMessage();
        List<TargetedMessage> targetedMessages = Lists.newArrayList();
        for (String target : targets) {
            TargetedMessage message = new TargetedMessage();
            message.setTarget(TargetedMessage.TARGET_TYPE_ALIAS, target);
            message.setMessage(buildMIMessage(pushMessage));
            targetedMessages.add(message);
        }
        return targetedMessages;
    }

    public static Message buildMiPushMessage(Notification notification) {
        return new Message.Builder()
                .notifyType(1)
                .passThrough(0)
                .restrictedPackageName(Notification.MI_PACKAGE_NAME)
                .title(Notification.NOTIFICATION_TITLE)
                .notifyId(notification.getPushMessage().getBadge())
                .description(notification.getPushMessage().getAlertMsg())
                .extra("url", notification.getPushMessage().getUrl())
                .build();
    }

    public static List<String> buildMiPushTarget(Notification notification) {
        List<String> targets = Lists.newArrayList();
        for (String target : notification.getTarget().split(",")) {
            targets.add(StringUtils.trimAllWhitespace(target));
        }
        return targets;
    }

    public static Message buildMIMessage(PushMessage pushMessage) {
        return new Message.Builder()
                .title(Notification.NOTIFICATION_TITLE)
                .description(pushMessage.getAlertMsg())
                .payload(pushMessage.getAlertMsg())
                .restrictedPackageName(Notification.MI_PACKAGE_NAME)
                .passThrough(0)
                .notifyType(1)
                .build();
    }
}
